package com.hezuti.info.service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hezuti.common.cache.Cache;
import com.hezuti.common.cache.dcache.DCached;
import com.hezuti.info.dao.CityDao;
import com.hezuti.info.dao.CountryDao;
import com.hezuti.info.dao.DistrictDao;
import com.hezuti.info.dao.ProvinceDao;
import com.hezuti.info.dao.SchoolDao;
import com.hezuti.info.domain.C;
import com.hezuti.info.domain.Country;
import com.hezuti.info.domain.P;
import com.hezuti.info.domain.Province;
import com.hezuti.info.domain.School;
import com.hezuti.info.domain.U;

@Service
public class InfoService {

	@Autowired
	private CountryDao countryDao;
	@Autowired
	private ProvinceDao provinceDao;
	@Autowired
	private CityDao cityDao;
	@Autowired
	private DistrictDao districtDao;
	@Autowired
	private SchoolDao schoolDao;
	
	
	@Cache(EntityName=String.class)
	public String getAllCollageByCountry() throws UnsupportedEncodingException{
		List<C> result=new ArrayList<C>();
		
		List<Country> cs=	countryDao.findAll();
		
		for(Country c:cs){
		 List<Province> ps=	provinceDao.findAllByCid(c.getId());
		 System.out.println(ps.size());
		 List<P> r2=new ArrayList<P>();
		 for(Province p:ps){
			 System.out.println(p.getId());
			 List<School> ss = schoolDao.findCollagesByPid(p.getId());
			 System.out.println(ss.size());
			 List<U> r3=new ArrayList<U>();
			 for(School s:ss){
				 r3.add(new U(s.getId(),s.getName()));
			 }
			 r2.add(new P(p.getId(),p.getName(),r3));
		 }
		 result.add(new C(c.getId(),c.getName(),r2));
		}
		String str=JSONArray.fromObject(result).toString();
		return str;
	}


}
